package cn.chenfei.sshd.client.ui;

import cn.chenfei.sshd.client.common.SettingUtils;
import cn.chenfei.sshd.client.common.SshConnect;
import cn.chenfeifx.core.common.ResourcesLoader;
import cn.chenfeifx.core.common.SimpleButton;
import cn.chenfeifx.core.common.SimpleControl;
import cn.chenfeifx.core.controls.CFForm;
import cn.chenfeifx.core.controls.CFTextField;
import cn.chenfeifx.core.enums.Level;
import javafx.geometry.HPos;
import javafx.geometry.Pos;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;

import java.util.List;

public class Home extends StackPane {

    // 表单
    private final CFTextField conName = new CFTextField("新连接");
    private final CFTextField conHost = new CFTextField("127.0.0.1");
    private final CFTextField conPort = new CFTextField("22");
    private final CFTextField conUser = new CFTextField("root");
    private final CFTextField conPassword = new CFTextField(CFTextField.Type.PASSWORD);
    private final SimpleButton submit = SimpleButton.get("连接", Level.PRIMARY);
    private final CFForm cfForm = new CFForm(10, 10);

    public Home() {
        initialize();
    }

    private void initialize() {
        getStyleClass().add("home");
        loadFormChildren();
        getChildren().add(cfForm);
    }

    private void loadFormChildren() {
        cfForm.add("连接名：", conName, 0);
        HBox hBox = new HBox(conHost, SimpleControl.getLabel("端口："), conPort);
        hBox.setSpacing(10);
        hBox.setAlignment(Pos.CENTER_LEFT);
        conHost.setPrefWidth(150);
        conPort.setPrefWidth(60);
        cfForm.add("主机：", hBox, 1);
        cfForm.add("用户名：", conUser, 2);
        cfForm.add("密码：", conPassword, 3);
        // 提交按钮
        cfForm.add(submit, 4);
        CFForm.setHalignment(submit, HPos.RIGHT);
        //
        submit.setOnMouseClicked(event -> {
            List<SshConnect> sshConnects = SettingUtils.listConnect();
            if (sshConnects.isEmpty()) {
                SettingUtils.saveConnect(getFormData());
            } else {
                SettingUtils.updateConnect(0, getFormData());
            }
        });
        List<SshConnect> sshConnects = SettingUtils.listConnect();
        if (!sshConnects.isEmpty()) {
            SshConnect sc = sshConnects.get(0);
            setFormText(sc.getName(), sc.getHost(), String.valueOf(sc.getPort()), sc.getUser(), sc.getPassword());
        }
    }

    private void setFormText(String name, String host, String port, String user, String password) {
        conName.setText(name);
        conHost.setText(host);
        conPort.setText(port);
        conUser.setText(user);
        conPassword.setText(password);
    }

    private SshConnect getFormData() {
        return new SshConnect()
                .setName(conName.getText())
                .setUser(conUser.getText())
                .setHost(conHost.getText())
                .setPort(Integer.parseInt(conPort.getText()))
                .setPassword(conPassword.getText());
    }

    @Override
    public String getUserAgentStylesheet() {
        return ResourcesLoader.load("/css/home.css");
    }
}
